<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/24
 * Time: 10:03
 */

namespace app\admin\model;


use app\admin\validate\TagValidate;
use app\common\model\BaseModel;
use app\Excepiton\DatabaseException;

class Tag extends BaseModel
{
    protected $table = 'tag';
    protected $pk = 'tid';


    /*
    *添加或编辑标签,编辑需要输入tid
    */
    public function addOrEditTag($tid){
        //2.获取提交信息
        $postData = (new TagValidate())->postCheckAndGet();
        //3.修改标签名需要添加条件..
        if ($tid){
            //更新标签
            $result = $this->update($postData,['tid'=>$tid]);
        }
        else{
            //新增加标签
            $result = $this->save($postData);
        }
        if (!$result){
            throw new DatabaseException();
        }
        return true;
    }

    /*
    *删除标签
    */
    public function del(){
        $tid = input('post.tid');
        $result = $this->where('tid','=',$tid)->delete();
        if (!$result){
            throw new DatabaseException();
        }
        return true;
    }


}